package com.ljh.booksysms.util;

import java.io.IOException;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


/**
 * Mybatis 的帮助类
 * @author ljh
 * @time 2016-12-6 下午6:23:03
 * @desc
 */
public class MyBatisUtil {
	
	/**
	 * @return
	 * 下午6:15:45
	 */
	public static SqlSession init(SqlSession session){
		System.out.println("初始化...");
		try {
			SqlSessionFactory ssfactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("MyBatis.xml"));
			session=ssfactory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return session;
	}
	
	
	public static void close(SqlSession session){
		System.out.println("关闭资源");
		
		if (null != session) {
			session.commit();
			session.close();
		}
	}
	
}
